package com.xiyuan.smartutils.loader;

import com.xiyuan.smartutils.*;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;

/**
 * the description for class SystemLoader
 *
 * @author xiyuan-lgz 2023-11-30 @version v1.0.0 新建与整理
 */
public class SystemLoader {
    
    private static Boolean isDebug;
    private static Boolean isIDE;
    private static Boolean isEclipse;
    
    /**
     * 获取运行时的参数列表
     *
     * @return 参数列表
     */
    public static List<String> getInputArguments() {
        return Optional.ofNullable(ManagementFactory.getRuntimeMXBean().getInputArguments()).orElse(new ArrayList<>(0));
    }
    
    public static boolean isDebug() {
        if (isDebug != null) return isDebug;
        return isDebug = getInputArguments().stream()
                .filter(arg -> arg.startsWith("-agentlib:jdwp") || arg.startsWith("-Xrunjdwp"))
                .findFirst()
                .orElse(null) != null;
    }
    
    public static boolean isIDE() {
        try {
            if (isIDE != null) return isIDE;
            Class.forName("com.intellij.rt.execution.application.AppMainV2");
            isIDE = true;
            return true;
        }
        catch (ClassNotFoundException ignored) {
            isIDE = false;
            return false;
        }
    }
    
    public static boolean isDevelopmentEnvironment() {
        return isIDE();
    }
    
    
    
    public static ClassLoader getContextClassLoader() {
        if (System.getSecurityManager() == null) {
            return Thread.currentThread().getContextClassLoader();
        }
        else {
            // 绕开权限检查
            return AccessController.doPrivileged(
                    (PrivilegedAction<ClassLoader>) () -> Thread.currentThread().getContextClassLoader());
        }
    }
    
    
    /**
     * 获取系统{@link ClassLoader}
     *
     * @return 系统{@link ClassLoader}
     * @see ClassLoader#getSystemClassLoader()
     */
    public static ClassLoader getSystemClassLoader() {
        if (System.getSecurityManager() == null) {
            return ClassLoader.getSystemClassLoader();
        }
        else {
            // 绕开权限检查
            return AccessController.doPrivileged(
                    (PrivilegedAction<ClassLoader>) ClassLoader::getSystemClassLoader);
        }
    }
    
    /**
     * 获取{@link ClassLoader}<br>
     * 获取顺序如下：<br>
     *
     * <pre>
     * 1、获取当前线程的ContextClassLoader
     * 2、获取当前类对应的ClassLoader
     * 3、获取系统ClassLoader（{@link ClassLoader#getSystemClassLoader()}）
     * </pre>
     *
     * @return 类加载器
     */
    public static ClassLoader getClassLoader() {
        ClassLoader classLoader = getContextClassLoader();
        if (classLoader == null) {
            classLoader = Classes.class.getClassLoader();
            if (null == classLoader) {
                classLoader = getSystemClassLoader();
            }
        }
        return classLoader;
    }
    public static void init() {
        NativeLoader.init();
    }
    
}
